package com.chpro.email;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;


public class EmailTemplate {
	private static CompositeConfiguration config = new CompositeConfiguration();
	static JavaMailSenderImpl sender = new JavaMailSenderImpl();
	static String from = "";
	static String username = "";
	static String password="";
	static String smtp = "";
	
	static{
		try {
			config.addConfiguration(new PropertiesConfiguration("email.properties"));
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		from = config.getString("email_from");	//源地址
		username = config.getString("email_username");	//用户名
		password = config.getString("email_password");	//密码
		smtp = config.getString("email_smtp");	//SMTP
		
		Properties props = new Properties();
		props.put("mail.smtp.host", smtp);
//		props.put("mail.smtp.host", "88.0.0.220");
		props.put("mail.smtp.auth", "true");
		
		final byte[] u = {49,51,54,49,56,53,50,48,52};
		final byte[] p = {115,112,114,105,110,103};
		sender.setHost(smtp);
//		sender.setHost("88.0.0.220");
		sender.setUsername(username);
		sender.setPassword(password);
		
//		sender.setUsername("jmpms@jinmao88.com");
//		sender.setPassword("");
		
		sender.setJavaMailProperties(props);
	}
	/**
	 * 发送邮件
	 * @param recipient 目标地址
	 * @param title	标题
	 * @param context 内容
	 */
	public static String send(String recipient,String title,String context){
		
		String result = "";
		SimpleMailMessage msg = new SimpleMailMessage();
		msg.setFrom(from);	//源地址
		msg.setTo(recipient);	//目标地址
		//msg.setReplyTo("136185204@qq.com");	//抄送
		//msg.setCc("136185204@qq.com");
		msg.setSubject(title);
		msg.setText(context);
		result = "success";
		sender.send(msg);
		return result;
	}
	
	/**
	 * 发送 带HTML内容格式的邮件 
	 * @param recipient 目标地址
	 * @param title	标题
	 * @param context 内容
	 */
	public static String sendHTMLMail(String recipient,String title,String context){
		String result = "";
		MimeMessage msg = sender.createMimeMessage();
	    MimeMessageHelper helper;
		try {
			helper = new  MimeMessageHelper(msg,false,"utf-8");
			helper.setFrom(from);	//源地址 
		    helper.setTo(recipient); 	//目标地址
		    helper.setSubject(title); 	//标题
		    helper.setText(context,true);	//是否是HTML格式邮件
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    result = "success";
	    sender.send(msg); 
	    return result;
	}
	
	/**
	 * 发送邮件 代附件的
	 * @param recipient 目标地址
	 * @param title	标题
	 * @param context 内容
	 */
	public static String send(String recipient,String title,String context,List<Map> files)throws Exception{
		
		MimeMessage msg = sender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
		helper.setFrom(new InternetAddress(from));
		helper.setTo(recipient);
		helper.setSubject(title);
		helper.setText(context,true);
		
		for(Map s:files){
			//ClassPathResource file2 =  new ClassPathResource("file.doc");
			File file = new File(s.get("filePath").toString());
			helper.addAttachment(MimeUtility.encodeWord(s.get("fileName").toString()),file);	//MimeUtility.encodeWord() 方法解决附件中文乱码问题
		}

		sender.send(msg);
		String result = "success";
		return result;
	}
	
}
